<template>
	<view class="uni-container">
		<uni-forms ref="form" :value="formData" validateTrigger="bind">
			<uni-forms-item name="s_id" label="题目编号">
				<uni-easyinput v-model="formData.s_id"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="q_type" label="题型">
				<uni-data-checkbox v-model="formData.q_type" :localdata="formOptions.q_type_localdata">
				</uni-data-checkbox>
			</uni-forms-item>
			<uni-forms-item name="q_desc" label="题目介绍">
				<uni-easyinput v-model="formData.q_desc"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item label="题目选项">


				<add-struct :quesction_type="formData.q_type" :quesction_struct="formData.q_struct"
					@updateValue="updatestruct" @addstruct="addstruct" @delstruct="delstruct">
				</add-struct>

			</uni-forms-item>
			<uni-forms-item name="q_ans" label="题目答案">
				<uni-easyinput v-model="formData.q_ans"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="img" label="试题图片">
				<uni-easyinput v-model="formData.img"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="video" label="试题视频讲解">
				<uni-easyinput v-model="formData.video"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="sound" label="视频音频讲解">
				<uni-easyinput v-model="formData.sound"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="auto_sound" label="试题读题语音">
				<uni-easyinput v-model="formData.auto_sound"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="q_analyse" label="试题文字解析">
				<uni-easyinput v-model="formData.q_analyse"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="key_word" label="关键字">
				<uni-easyinput v-model="formData.key_word"></uni-easyinput>
			</uni-forms-item>
			<view class="uni-button-group">
				<button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
				<navigator open-type="navigateBack" style="margin-left: 15px;">
					<button class="uni-button" style="width: 100px;">返回</button>
				</navigator>
			</view>
		</uni-forms>
	</view>
</template>

<script>
	import {
		validator
	} from '@/js_sdk/validator/quesction.js';

	const db = uniCloud.database();
	const dbCmd = db.command;
	const dbCollectionName = 'quesction';

	function getValidator(fields) {
		let result = {}
		for (let key in validator) {
			if (fields.includes(key)) {
				result[key] = validator[key]
			}
		}
		return result
	}

	export default {
		data() {
			let formData = {
				"s_id": "",
				"q_type": 0,
				"q_desc": "",
				"q_struct": [],
				"q_ans": "",
				"img": "",
				"video": "",
				"sound": "",
				"auto_sound": "",
				"q_analyse": "",
				"key_word": ""
			}
			return {
				formData,
				formOptions: {
					"q_type_localdata": [{
							"value": 0,
							"text": "单选"
						},
						{
							"value": 1,
							"text": "多选"
						},
						{
							"value": 2,
							"text": "判断"
						}
					]
				},
				rules: {
					...getValidator(Object.keys(formData))
				}
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			updatestruct(result) {
				this.formData.q_struct[result.index].value = result.value
			},
			addstruct(v) {
				console.log(v)
				if (!this.formData.q_struct) {
					this.formData.q_struct = new Array()
				}
				this.formData.q_struct.push(v)
			},
			delstruct(v) {
				this.formData.q_struct.splice(v, 1)
			},

			/**
			 * 验证表单并提交
			 */
			submit() {
				uni.showLoading({
					mask: true
				})
				this.$refs.form.validate().then((res) => {
					return this.submitForm(res)
				}).catch(() => {}).finally(() => {
					uni.hideLoading()
				})
			},

			/**
			 * 提交表单
			 */
			submitForm(value) {
				console.log(value)
				value.q_struct = this.formData.q_struct
					value.sid = Number(value.s_id)
				// 使用 clientDB 提交数据
				return db.collection(dbCollectionName).add(value).then((res) => {
					uni.showToast({
						title: '新增成功'
					})
					this.getOpenerEventChannel().emit('refreshData')
					setTimeout(() => uni.navigateBack(), 500)
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				})
			}
		}
	}
</script>
